package com.ruoyi.business.controller;

import java.math.BigDecimal;
import java.util.*;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.BusYjhzWea;
import com.ruoyi.business.service.IBusYjhzWeaService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 气象信息Controller
 * 
 * @author ruoyi
 * @date 2023-08-01
 */
@RestController
@RequestMapping("/yjhz/wea")
@Api(tags = "有机旱作-气象信息")
public class BusYjhzWeaController extends BaseController
{
    @Autowired
    private IBusYjhzWeaService busYjhzWeaService;


    /**
     * 查询气象信息列表
     */
    @GetMapping("/data")
    @ApiOperation("图表数据")
    public AjaxResult weaData(BusYjhzWea busYjhzWea)
    {
        startPage();
        List<BusYjhzWea> list = busYjhzWeaService.selectBusYjhzWeaList(busYjhzWea);
        Map<String , List> data = new HashMap<>();
        List<String> keyList = new ArrayList<>();
        List<BigDecimal> temperatureList = new ArrayList<>();
        List<BigDecimal> humidityList = new ArrayList<>();
        List<BigDecimal> co2List = new ArrayList<>();
        List<BigDecimal> phList = new ArrayList<>();

        list.forEach(w->{
            keyList.add(w.getTime()+":00");
            temperatureList.add(w.getTemprature());
            humidityList.add(w.getHumidity());
            co2List.add(w.getCo2());
            phList.add(w.getPh());
        });
        Collections.reverse(keyList);
        Collections.reverse(temperatureList);
        Collections.reverse(humidityList);
        Collections.reverse(co2List);
        Collections.reverse(phList);

        data.put("keyList",keyList);
        data.put("temperatureList",temperatureList);
        data.put("humidityList",humidityList);
        data.put("co2List",co2List);
        data.put("phList",phList);


        return AjaxResult.success(data);

    }


    /**
     * 查询气象信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BusYjhzWea busYjhzWea)
    {
        startPage();
        List<BusYjhzWea> list = busYjhzWeaService.selectBusYjhzWeaList(busYjhzWea);
        return getDataTable(list);
    }

    /**
     * 导出气象信息列表
     */
    @PreAuthorize("@ss.hasPermi('yjhz:wea:export')")
    @Log(title = "气象信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusYjhzWea busYjhzWea)
    {
        List<BusYjhzWea> list = busYjhzWeaService.selectBusYjhzWeaList(busYjhzWea);
        ExcelUtil<BusYjhzWea> util = new ExcelUtil<BusYjhzWea>(BusYjhzWea.class);
        util.exportExcel(response, list, "气象信息数据");
    }

    /**
     * 获取气象信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('yjhz:wea:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(busYjhzWeaService.selectBusYjhzWeaById(id));
    }

    /**
     * 新增气象信息
     */
    @PreAuthorize("@ss.hasPermi('yjhz:wea:add')")
    @Log(title = "气象信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusYjhzWea busYjhzWea)
    {
        return toAjax(busYjhzWeaService.insertBusYjhzWea(busYjhzWea));
    }

    /**
     * 修改气象信息
     */
    @PreAuthorize("@ss.hasPermi('yjhz:wea:edit')")
    @Log(title = "气象信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusYjhzWea busYjhzWea)
    {
        return toAjax(busYjhzWeaService.updateBusYjhzWea(busYjhzWea));
    }

    /**
     * 删除气象信息
     */
    @PreAuthorize("@ss.hasPermi('yjhz:wea:remove')")
    @Log(title = "气象信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(busYjhzWeaService.deleteBusYjhzWeaByIds(ids));
    }
}
